package com.bookshop.controller;

import com.bookshop.entity.*;
import com.bookshop.entity.response.R;
import com.bookshop.service.CartService;
//import com.tencentcloudapi.tbp.v20190311.models.ResponseMessage;
import com.bookshop.service.UserService;
import com.bookshop.util.GetIdUtil;
import com.bookshop.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private GetIdUtil getIdUtil;
    @Resource
    private CartService cartService;
    @Resource
    private UserService userService;
    @Autowired
    public CartController(CartService cartService , UserService userService)
    {
        this.cartService = cartService;
        this.userService = userService;
    }
    @PostMapping("/addToCart")
    public R addToCart(@RequestBody Cart cart , HttpServletRequest request)
    {
        Integer id = getIdUtil.getId(request);
        cart.setUser_id(id);
        //返回结果
        int result = cartService.addToCart(cart);
        R responseMessage;

        if (result != 0)
        {
            responseMessage = R.ok();
        }
        else
        {
            responseMessage = R.error();
        }
        return responseMessage;
    }
    @PostMapping("/updateCart")
    public R updateCart(@RequestBody Cart cart , HttpServletRequest request)
    {
        Integer id = getIdUtil.getId(request);
        cart.setUser_id(id);
        int result = cartService.updateCart(cart);
        R responseMessage;

        if (result != 0)
        {
            responseMessage = R.ok();
        }
        else
        {
            responseMessage = R.error();
        }
        return responseMessage;
    }

    @PostMapping("/deleteFromCart")
    public R deleteFromCart(@RequestBody  List<Integer> bookIds , HttpServletRequest request)
    {
        Integer id = getIdUtil.getId(request);
        int result = cartService.deleteFromCart(id, bookIds);
        R responseMessage;

        if (result != 0)
        {
            responseMessage = R.ok();
        }
        else
        {
            responseMessage = R.error();
        }
        return responseMessage;
    }
    @PostMapping("/searchAllBook")
    public R searchAllCart(HttpServletRequest request)
    {
        Integer id = getIdUtil.getId(request);

        ArrayList<CartWithBook> booksByPublisher = cartService.searchAllCart(id);

        if(booksByPublisher == null)
        {
            return R.error().message("购物车为空！");
        }

        return R.ok().put("booksByPublisher" , booksByPublisher);
    }
    @PostMapping("/addToFavorite")
    public R addToFavorite(@RequestParam int bookId , HttpServletRequest request)
    {
        Integer userId = getIdUtil.getId(request);
        int result = cartService.addToFavorite(userId , bookId);

        if (result == 0)
        {
            return R.error().message("添加失败!");
        }
        return R.ok().message("添加成功!");
    }
}
